package com.htt.app.xonefm.jsonparsing;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.htt.app.xonefm.config.Config;
import com.htt.app.xonefm.object.Show;

public class JSONParser {
	
/**
 * function return list show from server
 */
public List<Show> getListShow(JSONObject jsonObject) {
	
	if(jsonObject == null)
		return null;
	
	List<Show> list = new ArrayList<Show>();
	
	try {
	    // Getting Array
	    JSONArray jsonArray = jsonObject.getJSONArray(Config.TAG_ListCategoryListenedsResult);
	    // looping through All show
	    for(int i = 0; i < jsonArray.length(); i++) {
	        JSONObject c = jsonArray.getJSONObject(i);
	        
	        // creata new Show
	        Show show = new Show();
	        
	        // adding values for Show
	        if(c.has(Config.TAG_CateId))
	        	show.setId(c.getInt(Config.TAG_CateId) + i);
	        if(c.has(Config.TAG_Name))
	        	show.setName(c.getString(Config.TAG_Name));
	        if(c.has(Config.TAG_Desc))
	        	show.setDesc(c.getString(Config.TAG_Desc));
	        if(c.has(Config.TAG_Url))
	        	show.setSource(c.getString(Config.TAG_Url));
	        
	        // adding Show to List
	        list.add(show);
	    }
	    
	} catch (JSONException e) {
	    e.printStackTrace();
	}
	
	return list;
}

}
